﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Linq;

/// <summary>
///JGJXDZGL 的摘要说明
/// </summary>
public  partial class JGJXDZGL
{
    public static JGJXDZGL GetByID(int id)
    {

        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            return context.JGJXDZGL.FirstOrDefault(row => row.ID == id);
        }
    }

    public static List<JGJXDZGL> GetAll()
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            return context.JGJXDZGL.ToList();
        }
    }

    public void Add()
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            context.JGJXDZGL.InsertOnSubmit(this);
            context.SubmitChanges();
        }
    }

    public void Update()
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            this.GetType().GetMethod("Detach").Invoke(this, null);
            var tbl = context.GetTable<JGJXDZGL>();
            tbl.Attach(this);
            context.Refresh(RefreshMode.KeepCurrentValues, this);
            context.SubmitChanges();
        }
    }

    public static List<JGJXDZGL> GetAllByConditon(int sheng, int shi, int qu, string txtMetaDes, int txtJGID)
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            var query = from t in context.JGJXDZGL
                        select t;
            if (sheng != 0)
                query = query.Where(row => row.ProvinceID == sheng);
            if (shi != 0)
                query = query.Where(row => row.CityID == shi);
            if (qu != 0)
                query = query.Where(row => row.ReginID == qu);
            if (!string.IsNullOrEmpty(txtMetaDes))
                query = query.Where(row => row.MetaDes.Contains(txtMetaDes));
            if (txtJGID != 0)
                query = query.Where(row => row.ID == txtJGID);
              //query = query.Where(row => row.JGID == txtJGID);
            query = query.OrderByDescending(row => row.Rank);
            return query.ToList();
        }
    }
}